﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace csResistanceCalculator
{
    public partial class Form1 : Form
    {
        float 	buffer = 0, answer = 0;
        int[] 	ResistorNumber = new int[5],
        		Resistor = new int[4];
        int		SwitchPosition = 2,
        		i;
        string 	Default = "",
       			szBuffer = "Ohms";
       
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            for (i = 0; i < 5; i++)
            {
                ResistorNumber[i] = i;
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // public methods
        public void SetTotalResistorNumber(int Position, int value)
        {
            Resistor[Position] = value;
        }

        public string GetTotalResistorNumber(int Position)
        {
            switch (Position)
            {
                case 0:
                case 1:
                    return Resistor[0].ToString() + Resistor[1].ToString();
                case 2:
                    return Resistor[0].ToString() + Resistor[1].ToString() + Resistor[2].ToString();
            }

            return "";
        }

        private void txtOhm11_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm11.Text == "" || txtOhm11.Text == "0" || txtOhm11.Text == "0.")
            {
                txt1DR11.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm11.Text);
                answer = 1 / buffer;
                txt1DR11.Text = answer.ToString();
            }
        }

        private void txtOhm12_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm12.Text == "")
            {
                txt1DR12.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm12.Text);
                answer = 1 / buffer;
                txt1DR12.Text = answer.ToString();
            }
        }

        private void txtOhm13_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm13.Text == "")
            {
                txt1DR13.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm13.Text);
                answer = 1 / buffer;
                txt1DR13.Text = answer.ToString();
            }
        }

        private void txtOhm14_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm14.Text == "")
            {
                txt1DR14.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm14.Text);
                answer = 1 / buffer;
                txt1DR14.Text = answer.ToString();
            }
        }

        private void txtOhm15_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm15.Text == "")
            {
                txt1DR15.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm15.Text);
                answer = 1 / buffer;
                txt1DR15.Text = answer.ToString();
            }
        }

        private void txtOhm16_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm16.Text == "")
            {
                txt1DR16.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm16.Text);
                answer = 1 / buffer;
                txt1DR16.Text = answer.ToString();
            }
        }

        private void txtOhm17_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm17.Text == "")
            {
                txt1DR17.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm17.Text);
                answer = 1 / buffer;
                txt1DR17.Text = answer.ToString();
            }
        }

        private void txtOhm18_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm18.Text == "")
            {
                txt1DR18.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm18.Text);
                answer = 1 / buffer;
                txt1DR18.Text = answer.ToString();
            }
        }

        private void txtOhm19_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm19.Text == "")
            {
                txt1DR19.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm19.Text);
                answer = 1 / buffer;
                txt1DR19.Text = answer.ToString();
            }
        }

        private void txtOhm110_TextChanged(object sender, EventArgs e)
        {
            buffer = 0;
            answer = 0;

            if (txtOhm110.Text == "")
            {
                txt1DR110.Text = "";
            }
            else
            {
                buffer = float.Parse(txtOhm110.Text);
                answer = 1 / buffer;
                txt1DR110.Text = answer.ToString();
            }
        }

        private void txt1DR11_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR12_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR13_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR14_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR15_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR16_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR17_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR18_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR19_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void txt1DR110_TextChanged(object sender, EventArgs e)
        {
            TotalParallelResistance1();
        }

        private void TotalParallelResistance1()
        {
            buffer = 0;
            answer = 0;

            if (txt1DR11.Text == "")
                txt1DRT.Text = "";
            else
                buffer = buffer + float.Parse(txt1DR11.Text);

            if (txt1DR12.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR12.Text);

            if (txt1DR13.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR13.Text);

            if (txt1DR14.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR14.Text);

            if (txt1DR15.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR15.Text);

            if (txt1DR16.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR16.Text);

            if (txt1DR17.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR17.Text);

            if (txt1DR18.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR18.Text);

            if (txt1DR19.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR19.Text);

            if (txt1DR110.Text == "")
                buffer = buffer + 0;
            else
                buffer = buffer + float.Parse(txt1DR110.Text);

            answer = 1 / buffer;
            txt1DRT.Text = answer.ToString();
        }

        private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            string buffer = "";

            buffer = comboBox1.Text;

            switch (buffer)
            {
                case "Volt":
                    richTextBox1.Text = "R · I\nP / R\n√P · R";
                    break;
                case "Resistance":
                    richTextBox1.Text = "V / I\nV² / P\nP / I²";
                    break;
                case "Ampere":
                    richTextBox1.Text = "√P / R\nP / V\nV / R";
                    break;
                case "Power":
                    richTextBox1.Text = "V² / R\nR · I²\nV · I";
                    break;
            }
        }

        private void btnDigit1_0_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 0);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_1_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 1);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_2_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 2);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_3_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 3);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_4_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 4);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_5_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 5);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_6_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 6);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_7_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 7);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_8_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 8);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit1_9_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(0, 9);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }
        
        private void btnDigit2_0_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 0);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_1_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 1);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_2_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 2);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_3_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 3);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_4_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 4);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_5_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 5);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_6_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 6);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_7_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 7);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_8_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 8);
            if (SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if (SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit2_9_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(1, 9);
            if(SwitchPosition == 2)
                buffer = GetTotalResistorNumber(1);
            else if(SwitchPosition == 3)
                buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
        }

        private void btn100ppm_Click(object sender, EventArgs e)
        {
            float number, ppm, buffer;

            number = 100;
            ppm = 1000000;
            buffer = (long.Parse(Default) / ppm) * number;
            txtPPM.Text = buffer.ToString();
        }

        private void btnDigit3_0_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 0);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_1_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 1);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_2_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 2);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_3_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 3);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_4_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 4);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_5_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 5);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_6_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 6);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_7_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 7);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_8_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 8);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnDigit3_9_Click(object sender, EventArgs e)
        {
            string buffer = "";
            SetTotalResistorNumber(2, 9);
            buffer = GetTotalResistorNumber(2);
            txtDigit.Text = buffer;
            Default = buffer;
        }

        private void btnMultiplier001_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 0.01);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = szBuffer;
        }

        private void btnMultiplier01_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 0.1);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = szBuffer;
        }

        private void btnMultiplier1_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 1);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = szBuffer;
        }

        private void btnMultiplier10_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 10);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = szBuffer;
        }

        private void btnMultiplier100_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 100);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = szBuffer;
        }

        private void btnMultiplier1K_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;
           
            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 1);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = "K" + szBuffer;
        }

        private void btnMultiplier10K_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 10);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = "K" + szBuffer;
        }

        private void btnMultiplier100K_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 100);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = "K" + szBuffer;
        }

        private void btnMultiplier1M_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 1);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = "M" + szBuffer;
        }

        private void btnMultiplier10M_Click(object sender, EventArgs e)
        {
            int iBuffer = 0;
            double dBuffer = 0.0;

            iBuffer = int.Parse(Default);
            dBuffer = (iBuffer * 10);
            txtDigit.Text = dBuffer.ToString();
            lblOhms.Text = "M" + szBuffer;
        }
        
        void Rbtn2digitCheckedChanged(object sender, EventArgs e)
        {
        	string NumDigits = "";
        	
        	NumDigits = GetTotalResistorNumber(1);
            txtDigit.Text = NumDigits.ToString();
            SwitchPosition = 2;

            btnDigit3_0.Visible = false;
            btnDigit3_1.Visible = false;
            btnDigit3_2.Visible = false;
            btnDigit3_3.Visible = false;
            btnDigit3_4.Visible = false;
            btnDigit3_5.Visible = false;
            btnDigit3_6.Visible = false;
            btnDigit3_7.Visible = false;
            btnDigit3_8.Visible = false;
            btnDigit3_9.Visible = false;
        }
        
        void Rbtn3digitCheckedChanged(object sender, EventArgs e)
        {
        	string NumDigits = "";
        	
        	NumDigits = GetTotalResistorNumber(2);
            txtDigit.Text = NumDigits.ToString();
            SwitchPosition = 3;

            btnDigit3_0.Visible = true;
            btnDigit3_1.Visible = true;
            btnDigit3_2.Visible = true;
            btnDigit3_3.Visible = true;
            btnDigit3_4.Visible = true;
            btnDigit3_5.Visible = true;
            btnDigit3_6.Visible = true;
            btnDigit3_7.Visible = true;
            btnDigit3_8.Visible = true;
            btnDigit3_9.Visible = true;
        }
        
        void TabPage2Click(object sender, EventArgs e)
        {
        	
        }
        
        void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
        {
        	
        }
    }
}